package bcexchanger.ui;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;

import bcexchanger.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Display;
import org.netbeans.microedition.lcdui.SimpleTableModel;
import org.netbeans.microedition.lcdui.pda.FileBrowser;

/**
 * 
 * This class represents a screen displaying the list of contacts of
 * the address book. The user can either choose the one contact or
 * exit the application
 * 
 * This class peforms the reading of address book entries in the 
 * separate thread, because reading the long address book might
 * take significant time
 * 
 * The class is extending an example.BCExchanger.ui.Screen class and
 * implements Runnable interface.
 * 
 * @see example.BCExchanger.ui.Screen Design patterns: State
 */
public class FilesScreen extends Screen implements Runnable {

  private List list;
  private Command okCommand;
  private Command exitCommand;

  private Gauge gauge;
  private Form progressBar;
  private Thread thread;
  private Command progressExitCommand;
    
  private FileBrowser fileBrowser;
  private FileConnection selectedFile;
  
  /**
   * Constructor
   * 
   * @param _midlet -
   *          the parent class which keeps the current UI state
   */
  public FilesScreen(BCExchangerMIDlet _midlet) {
    super(_midlet);
   
    // create and add commands to ui list
    progressBar = new Form("Please wait");

    gauge = new Gauge("Buscando Directorios", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
    gauge.setLayout(Item.LAYOUT_CENTER);
    gauge.setLayout(Item.LAYOUT_VCENTER);
    progressBar.append(gauge);
    
    progressExitCommand = new Command("Exit", Command.EXIT, 1);
    progressBar.addCommand(progressExitCommand);

    progressBar.setCommandListener(this);

    displayable = progressBar;


    // start the thread reading the address book entries
    thread = new Thread(this);
    thread.start();
    
  }
  
  

  public void commandAction(Command command, Displayable _displayable) {

    // in case this callback is called from wrong displayble
    midlet.changeScreen(new AlertMessage(midlet,
            "Comandos!", null));
      
      
    if (command == FileBrowser.SELECT_FILE_COMMAND) 
    {                                         
        // write pre-action user code here
        try
        {
            selectedFile = fileBrowser.getSelectedFile();
            midlet.setSelectedFile(getSelectedFile());
            midlet.changeScreen(new SelectFileScreen(midlet, this));
        }
        catch(Exception e)
        {
           midlet.changeScreen(new AlertMessage(midlet,
            e.getMessage(), null));
        }
        // write post-action user code here
    } 
                                                     
    else if (command == exitCommand || command == progressExitCommand) {
      midlet.quit();
    } else {
      // unknown command
      throw new RuntimeException("Internal error #20");
    }
  }

 
  
  public FileBrowser getFileBrowser() {
        if (fileBrowser == null) {                                 
            // write pre-init user code here
            fileBrowser = new FileBrowser(Display.getDisplay(midlet));                                    
            fileBrowser.setTitle("fileBrowser");
            fileBrowser.setCommandListener(this);
            fileBrowser.addCommand(FileBrowser.SELECT_FILE_COMMAND);
            Command cmdCancelBrowser = new Command("Cancelar", Command.CANCEL, 0);                                   
            fileBrowser.addCommand(cmdCancelBrowser);               
            // write post-init user code here
        }                         
        return fileBrowser;
    }
  
  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  public void run() {
    
      // create and add commands to ui list

      displayable = getFileBrowser();

      midlet.changeScreen(this);
	
    }
  
  public FileConnection getSelectedFile()
  {
      return selectedFile;
  }
}
